var PROJECT_PATH = "";
var ARCHIVE_PATH = "";
function setPathPrefix(projectPath, archivePath) {
	PROJECT_PATH = projectPath;
	ARCHIVE_PATH = archivePath;
}
$(function() {
	// 初始化select2控件
	$('#archiveType').select2({
		placeholder : '档案类型'
	});
	// 绑定select2 负责人控件
	WASP_WIDGET.triggerSelectOnEmpId("#responsibleId", {
		multiple : false
	});

	// 对select控件绑定验证事件
	triggerValidOnSelectChange('dctIsLeaf', 'dctStat');
	// 初始化窗口标题名称
	setTitleName();

});

var ARCHIVE = {
	// 选择系列产品
	selectProject : function() {
		var actionUrl = PROJECT_PATH + "selectProjectView.do";
		openDialog(actionUrl);
	},
	// 检查form表单
	checkForm : function() {
		// 需要提交的数据：产品代码、旧的产品代码、附件主键、拓展字段、拓展字段信息
		var prjCode = $("#prjCode").val();
		var oldPrjCode = $("#oldPrjCode").val();
		var archiveInfoJson = [];
		var attachIds = $("input[name='attId']");
		//获取当前模块附件删除列表,$("#attDelIdsArchiveAddView")该ID是通过：attDelIds + <jsp:param name="module" value="ArchiveAddView" /> 配置的ArchiveAddView
		var attDelIds = $("#attDelIdsArchiveAddView").val();

		if (!checkValue(prjCode)) {
			ctools.alert("请选择产品信息！", "", "warning");
			return;
		}
		if (!checkValue(attachIds)) {
			ctools.alert("请至少上传1个附件！", "", "warning");
			return;
		}
		attachIds.each(function() {
			var attachRelObject = {};
			var attachId = $(this).val();
			var extId = $("#archiveTypeExtId"+attachId).val();
			var expandField = "archiveType" + attachId;
			var expandValue = $("#" + expandField).val();
			
			if (!checkValue(expandValue)) {
				ctools.alert("请选择档案类型！", "", "warning");
				return;
			}
			attachRelObject.relId = prjCode;
			attachRelObject.oldPrjCode = oldPrjCode;
			attachRelObject.attId = attachId;
			attachRelObject.attrName = "archiveType";
			attachRelObject.attrValue = expandValue;
			attachRelObject.relType = "ARCHIVE_ADD";
			attachRelObject.attDelId = attDelIds;
			
			// extId为空
			if(extId){
				attachRelObject.extId = extId;
			}
			archiveInfoJson.push(attachRelObject);
		});
		return archiveInfoJson;
	},
	// 新增档案信息
	addArchive : function() {
		var attachIds = $("input[name='attId']");
		var archiveInfoJson = ARCHIVE.checkForm();
		if (!archiveInfoJson || archiveInfoJson.length == 0 || !attachIds || attachIds.length != archiveInfoJson.length) {
			return;
		}
		// 将信息存入后台
		var actionUrl = ARCHIVE_PATH + "saveArchive.do";
		$.ajax({
			type : "POST",
			url : actionUrl,
			data : JSON.stringify(archiveInfoJson),
			dataType : "json",
			contentType : "application/json",
			error : function(e) {
				toastr.error(e, "保存失败");
			},
			complete : function(XMLHttpRequest, textStatus){
				$("body").html(XMLHttpRequest.responseText);
		    }
		});
	},
	// 修改档案信息
	updateArchive : function() {
		var attachIds = $("input[name='attId']");
		var archiveInfoJson = ARCHIVE.checkForm();
		if (!archiveInfoJson || archiveInfoJson.length == 0 || !attachIds || attachIds.length != archiveInfoJson.length) {
			return;
		}
		// 将信息存入后台
		var actionUrl = ARCHIVE_PATH + "updateArchive.do";
		$.ajax({
			type : "POST",
			url : actionUrl,
			data : JSON.stringify(archiveInfoJson),
			dataType : "json",
			contentType : "application/json",
			cache : false,
			error : function(e) {
				toastr.error(e, "保存失败");
			},
			complete : function(XMLHttpRequest, textStatus){
				$("body").html(XMLHttpRequest.responseText);
		    }
		});
	}
}
// 设置窗口标题名称
function setTitleName() {
	var titleName = "档案管理新增";
	var actionType = $("#actionType").val();
	if ("detail" == actionType) {
		titleName = "档案管理详情";
		$('input').attr("readOnly", "readOnly");
		$('button').hide();
		$('select').attr("disabled", "true");
	} else if ("update" == actionType) {
		titleName = "档案管理修改";
	}
	$('*[name="titleName"]').html(titleName);
}

// 选择产品回调函数
function selectProjectCallback(prjcode, prjsname) {
	$("#prjCode").val(prjcode);
	$("#prjsname").val(prjsname);
}

// 检查字段是否为空
function checkValue(value) {
	if (value && value.length > 0) {
		return true;
	}
	return false;
}
// 刷新档案管理列表
function fleshParentGrid() {
	if (window.opener != null) {
		if (window.opener.document.btnRefresh != null) {
			window.opener.btnRefresh.click();
			if (window.opener.queryByCondtion != null) {
				window.opener.queryByCondtion(false);
			}
		} else {
			if (window.opener.queryByCondtion != null) {
				window.opener.queryByCondtion(false);
			}
		}
	}
}
